#include "menudataitem.h"
#include "ui_menudataitem.h"
#include <QDebug>
#include "GameData/gamedata.h"
MenuDataItem::MenuDataItem(DataType dataType):
    ui(new Ui::MenuDataItem)
{
    void (QDoubleSpinBox::*dvc)(double) = &QDoubleSpinBox::valueChanged;
    void (QSpinBox::*vc)(int) = &QSpinBox::valueChanged;
    ui->setupUi(this);
    this->dataType = dataType;
    switch(dataType)
    {
        case _INT:
            spinBox = new QSpinBox();
            spinBox->setMaximum(INT_MAX);
            spinBox->setMinimum(INT_MIN);
            ui->horizontalLayout_4->addWidget(spinBox);
            connect(spinBox,vc,this,[=](int i){
                GameData::Instance().intData[name] = i;
                emit EventCenter::Instance().Modify();
            });
        break;
        case _STRING:
            lineEdit = new QLineEdit();
            lineEdit->setPlaceholderText("输入文字");
            ui->horizontalLayout_4->addWidget(lineEdit);
            connect(lineEdit,&QLineEdit::editingFinished,this,[=](){
                GameData::Instance().stringData[name] = lineEdit->text();
                emit EventCenter::Instance().Modify();
            });
        break;
        case _FLOAT:
            doubleSpinBox = new QDoubleSpinBox();
            doubleSpinBox->setMaximum(INT_MAX);
            doubleSpinBox->setMinimum(INT_MIN);
            ui->horizontalLayout_4->addWidget(doubleSpinBox);
            connect(doubleSpinBox,dvc,this,[=](double d){
                GameData::Instance().floatData[name] = d;
                emit EventCenter::Instance().Modify();
            });
        break;
        case _BOOL:
            radioButton = new QRadioButton();
            ui->horizontalLayout_4->addWidget(radioButton);
            connect(radioButton,&QRadioButton::clicked,this,[=](bool b){
                GameData::Instance().boolData[name] = b;
                emit EventCenter::Instance().Modify();
            });
        break;
        default:break;
    }
    //删除
    connect(ui->DeleteBtn,&QPushButton::clicked,this,[=](){
        GameData::Instance().DeleteData(name);
        emit EventCenter::Instance().DeleteData(name);
        emit EventCenter::Instance().Modify();
    });
    connect(ui->nameEdit,&QLineEdit::editingFinished,this,[=](){
        QString newName = ui->nameEdit->text();
        if(GameData::Instance().ChangeName(name,newName))
        {
            name = newName;
            emit EventCenter::Instance().Modify();
        }
        else
            ui->nameEdit->setText(name);
    });
    //修改可见性
    connect(ui->checkBox,&QCheckBox::toggled,this,[=](bool checked){
        GameData::Instance().visible[ui->nameEdit->text()] = checked;
    });
}

MenuDataItem::~MenuDataItem()
{
    delete ui;
    delete lineEdit;
    delete doubleSpinBox;
    delete spinBox;
    delete radioButton;
}

void MenuDataItem::SetName(QString s)
{
    ui->nameEdit->setText(s);
    name = s;
    ui->checkBox->setChecked(GameData::Instance().visible[s]);
}

void MenuDataItem::ShowValue(QString s)
{
    switch(dataType)
    {
        case _INT:
            if(spinBox)
                spinBox->setValue(GameData::Instance().intData[s]);
        break;
        case _STRING:
            if(lineEdit)
                lineEdit->setText(GameData::Instance().stringData[s]);
        break;
        case _FLOAT:
            if(doubleSpinBox)
                doubleSpinBox->setValue(GameData::Instance().floatData[s]);
        break;
        case _BOOL:
            if(radioButton)
                radioButton->setChecked(GameData::Instance().boolData[s]);
        break;
        default:break;
    }
}

QString MenuDataItem::GetName()
{
    return name;
}
